const http = require('http');
const urllib = require('url');

const port = 8081;

// response的数据
const data = {
  'data': 'world'
};

// const sleep = (ms) => new Promise(res => setTimeout(res, ms));

const instance = http.createServer(async (req, res) => {
  const params = urllib.parse(req.url, true);
  if (params.query?.callback) {
    const str = `${params.query.callback}(${JSON.stringify(data)})`;
    res.end(str);
  } else {
    res.end()
  }
});

instance.listen(port, () => {
  console.log(`jsonp server is on: ${port}`)
})